/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-05-08 15:22:57
 * @LastEditors: DESKTOP-D0D7KKT\Administrator 18368095041@163.com
 * @LastEditTime: 2024-11-29 15:24:58
 * @FilePath: \vue3-admin\src\utils\auth.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import type { UserState } from '@/typings/store/user';
import Cookie from 'js-cookie';
import { TOKEN_KEY } from '@/enums/CacheEnum';

export function getToken() {
    return window.localStorage.getItem(TOKEN_KEY);
}

export function setToken(token: string) {
    return window.localStorage.setItem(TOKEN_KEY, token);
}

export function removeToken() {
    return window.localStorage.removeItem(TOKEN_KEY);
}

/**
 * 用于图片
 * @param token
 * @returns
 */
export function setCookieToken(token: string) {
    return Cookie.set('token', token);
}

export const setUserInfo = (userInfo: UserState['userInfo']) => {
    window.localStorage.setItem('userInfo', JSON.stringify(userInfo));
};

export const removeUserInfo = () => {
    window.localStorage.removeItem('userInfo');
};

export const getUserInfo = (): UserState['userInfo'] => {
    const res = window.localStorage.getItem('userInfo');
    return (res && JSON.parse(res)) || null;
};
